<?php
	/*
	 * AJAX call to load the list of banned SMS messages.
	 * Uses variables from settings database table, as well as filters (content and phonenumber).
	 *
	 * Variables:
	 *   none
	 *
	 * Returns: xml
	 *
	 */

	header('Content-type: text/xml');
	$lastd = preg_replace("/\D/","",$settings["party_date"]." ".$settings["party_hour"].":00");


	$sql = mysql_query("SELECT * FROM filterwords ORDER BY fwText", $db);
	$filters_words = array();
	while($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
		array_push($filters_words, $row["fwText"]);
	}

	$sql = mysql_query("SELECT * FROM filternumbers ORDER BY fnNR", $db);
	$filters_numers = array();
	while($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
		array_push($filters_numers, $row["fnNR"]);
		array_push($filters_numers, "+".$row["fnNR"]);
	}

	$smssql = mysql_query("SELECT * FROM inbox WHERE UpdatedInDB > $lastd ".(($settings["ban_by_number"] or $settings["ban_by_content"])?
		" AND (".
		($settings["ban_by_number"]?" SenderNumber IN (\"".implode("\",\"", $filters_numers)."\") ":"213=321").
		($settings["ban_by_content"]?" OR TextDecoded LIKE \"%".implode("%\" OR TextDecoded LIKE \"%", $filters_words)."%\"":"")
		.") ":"")." ORDER BY ID");


	echo '<root>';
	$sms = array();
	while($row = mysql_fetch_array($smssql, MYSQL_ASSOC)){
		echo "\n".'	<sms time="'.substr($row["ReceivingDateTime"], 5, 11).'" id="'.$row["ID"].'" from="'.$row["SenderNumber"].'"><![CDATA['.utf8_encode($row["TextDecoded"]).']]></sms>';
	}
	echo "\n".'</root>';

?>